package com.test.mywebtest.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ssc.utils.AccessSignUtil;
import com.test.mywebtest.config.R;
import com.test.mywebtest.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/rest")
public class OFDQZController {

    @Autowired
    private RestTemplate restTemplate;

    private String getToken = "http://192.168.82.149:8033/prod-api/openapi/v2/token/app_access_token";
    private String refresh_token = "http://192.168.82.149:8033/prod-api/openapi/v2/token/refresh_access_token";
    private String yysq = "http://192.168.82.149:8033/prod-api/openapi/v2/sign/applyStamp";

    //ofd
    //String sysCode = "47JKT4fw6tNLqrjy";
    //String sysSecret = "216191e2-7c25-445a-a4b9-aa4075e8d2d6";

    //本地pdf

    String sysCode = "73da4dfd-2ac0-4ebe-b243-4cea2c8d74f6";
    String sysSecret = "8OsjdKdZiJCmkP2I";




    @RequestMapping(value = "getToken", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    public R getToken() {
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put("sysCode",sysCode);
        objectNode.put("sysSecret",sysSecret);


        String url = getToken;
        HttpHeaders headers = new HttpHeaders();
        headers.set("requestId", UuidUtil.get32UUID());
        headers.set("timeStamp", String.valueOf(System.currentTimeMillis()));
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<?> entity = new HttpEntity<>(objectNode.toString(),headers);

        // 发送POST请求并获取返回结果
        ResponseEntity<String> response = restTemplate.exchange(url,
                HttpMethod.POST,
                entity,
                String.class);
        String body = response.getBody();
        // 输出返回结果
        System.out.println("Status Code: " + response.getStatusCode());
        System.out.println("Status Code Value: " + response.getStatusCodeValue());
        System.out.println("Body: " + body);

        return R.data(body);
    }

    @RequestMapping(value = "shenqing", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    public R shenqing(String token,String jsonData){
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode postParam = objectMapper.createObjectNode();
        postParam.put("applyNo", UuidUtil.get32UUID());//唯一标识
        //postParam.put("applyType", "");//类型： 1公文 2其他 默认1
        //postParam.put("hideFile", "");//是否隐藏左侧文件：1隐藏 0显示

        ObjectNode userOn = objectMapper.createObjectNode();//申请人信息
        userOn.put("userId", "010101");//用户编号
        userOn.put("userName", "测试张三");//用户姓名
        userOn.put("userDeptId", "020202");//用户所属部门编号
        userOn.put("userUnitId", "030303");//用户所属单位编号
        userOn.put("userDeptName", "测试部门1");//用户所属部门名称
        userOn.put("userUnitName", "测试单位1");//用户所属单位名称

        postParam.set("user", userOn);

        ArrayNode fileListAn = objectMapper.createArrayNode();//文件数组
        ObjectNode fileOn = objectMapper.createObjectNode();
        fileOn.put("fileOuterId","24e607f2fa474af08cc40e50a1c382c1");//文件唯一标识
        //fileOn.put("fileName","测试国产签章.pdf");//文件名
        //fileOn.put("fileGetType",1);//文件获取方式 1URL下载 2base64编码 3服务器物理文件路径 4客户端文件路径
        //fileOn.put("fileKind",2);//文件格式 1ofd 2pdf
        //fileOn.put("fileContext","http://192.168.81.173:18067/carsoa-dfs/fileDfs/v1/downloadFileDfs?objectId=24e607f2fa474af08cc40e50a1c382c1&token=ffc707b442214149aa98c9aba69c4be8");//文件下载地址/base64编码/服务器文件路径
        //fileOn.put("fileName","test_exp.ofd");//文件名
        fileOn.put("fileName","测试文档.pdf");//文件名
        fileOn.put("fileGetType",1);//文件获取方式 1URL下载 2base64编码 3服务器物理文件路径 4客户端文件路径
        fileOn.put("fileKind",2);//文件格式 1ofd 2pdf
        //fileOn.put("fileContext","http://192.168.81.173:19627/dfs/minio/v1/downloadFile?objectId=e9757439d29e42019fd3095664a40814&token=090c9cbbed18466a8785e62897cf3096");//文件下载地址/base64编码/服务器文件路径
        fileOn.put("fileContext","http://192.168.81.173:18067/carsoa-dfs/fileDfs/v1/downloadFileDfs?objectId=25d426185eb34df7affff8308b52dc21&token=0792117fcea848299ef12a1ff84b9977");//文件下载地址/base64编码/服务器文件路径
        //fileOn.put("digestType","");//文件摘要加密类型：1md5 2sm3 默认0不加密
        //fileOn.put("fileDigest","");//文件摘要加密串
        //fileOn.put("fileGroup","");//文件分类：正文 附件
        fileOn.put("needSign",1);//是否需要盖章：1需要 0不需要
        //fileOn.put("batchSign","");//是否需要批量盖章：1需要 0不需要 默认0
        fileOn.put("needDecrypt",2);//签章后是否需要脱密：0不需要 2无感脱密
        fileOn.put("needEdge",0);//是否需要加盖骑缝章：1需要 0不需要
        fileOn.put("sealAuth",0);//是否需要限制文件用印：0不需要 1需要 2授权的印章
        fileOn.put("sealTotal",1);//文件用印总数
        fileOn.put("callType",3);//反馈接口种文件传输类型：1base64 2url 3multipartFile 4使用callUrl（客户端直接推送） 默认1
        //fileOn.put("callUrl","");//反馈文件地址（multipartFile形式-其他字段base64加密串）callType=4时必传
        fileOn.put("encodeType",0);//盖章后加密类型：1硬加密，2硬+证书 3软加密 0不加密
        //fileOn.put("encodeWay","");//盖章后加密方式（默认1）1需要调用相关接口查看 2书生阅读器可直接打开（仅ofd文件）
        //fileOn.put("checkCertUnit", "");//是否检查单位证书：0不检查 1检查 默认1（传0时无证书会自动转为硬加密类型）

        //ArrayNode decryptListAn = objectMapper.createArrayNode();//脱密信息
        //ObjectNode decryptListOn = objectMapper.createObjectNode();
        //decryptListOn.set("type", "");//脱密类型：1黑头黑章 2红头红章 3转pdf
        ////decryptListOn.set("encodeType", "");//脱密后加密类型：1硬加密，2硬+证书 3软加密 0不加密 默认0
        ////decryptListOn.set("encodeWay","");//盖章后加密方式（默认1）1需要调用相关接口查看 2书生阅读器可直接打开（仅ofd文件）
        //decryptListAn.add(decryptListOn);
        //fileOn.set("decryptList",decryptListAn);

        //ArrayNode certUnitListAn = objectMapper.createArrayNode();//证书所属单位信息：encodeType=2或decryptList.encodeType=2时必传
        //ObjectNode certUnitListOn = objectMapper.createObjectNode();
        //certUnitListOn.set("unitCode", "");//证书所属单位编码
        //certUnitListAn.add(certUnitListOn);
        //fileOn.set("certUnitList",certUnitListAn);

        //ArrayNode sealListAn = objectMapper.createArrayNode();//文件用印数组（fileList.sealAuth=1时必填）
        //ObjectNode sealListOn = objectMapper.createObjectNode();
        //sealListOn.set("sealNo", "");//印章编码
        //sealListOn.set("useNum", "");//用印数量
        //sealListOn.set("sealName", "");//印章名称
        //sealListOn.set("signType", "");//用印类型：1拖拉拽
        //sealListOn.set("needEdge", "");//是否需要加盖骑缝章 1需要 0不需要
        //sealListAn.add(sealListOn);
        //fileOn.set("sealList",sealListAn);



        fileListAn.add(fileOn);
        postParam.set("fileList", fileListAn);


        //公文系统参数
        postParam.put("applyExt", jsonData);


        System.out.println("用印参数："+postParam.toString());
        String url = yysq;
        HttpHeaders headers = new HttpHeaders();
        headers.set("requestId", UuidUtil.get32UUID());
        headers.set("timeStamp", String.valueOf(System.currentTimeMillis()));
        headers.add("accessToken",token);
        headers.add("accessSign", AccessSignUtil.sign("/prod-api/openapi/v2/sign/applyStamp",postParam.toString(),sysSecret));
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<?> entity = new HttpEntity<>(postParam.toString(),headers);


        // 发送POST请求并获取返回结果
        ResponseEntity<String> response = restTemplate.exchange(url,
                HttpMethod.POST,
                entity,
                String.class);
        String body = response.getBody();
        // 输出返回结果
        System.out.println("Status Code: " + response.getStatusCode());
        System.out.println("Body: " + body);

        return R.data(body);
    }



    @RequestMapping(value = "feedback", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    public String feedback(MultipartFile file,String json){
        System.out.println(file.getOriginalFilename());
        System.out.println("反馈接口请求json:"+json);

        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode result = objectMapper.createObjectNode();
        result.put("code",200);
        result.set("data",null);
        result.put("ok",true);
        result.set("msg",null);

        return result.toString();

    }






}
